package come.bite.spring.demo;

import ch.qos.logback.core.util.StringUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    List<MessageInfo> messageInfoslist = new ArrayList<>(); //将对象放入容器里面

    @PostMapping(value = "/publish", produces = "application/json")
     public String publish(@RequestBody MessageInfo messageInfo) {   //字符串转json  如果是对象就不用在转成json了
        if(!StringUtils.hasLength(messageInfo.getFrom())             // @RequestBody 代表传递的是json对象
                ||!StringUtils.hasLength(messageInfo.getTo())||
                !StringUtils.hasLength(messageInfo.getMessage())){
            return "{\"ok\":0}";
        }
        messageInfoslist.add(messageInfo);
        return "{\"ok\":1}";
    }

    @GetMapping("/getlist")
    public List<MessageInfo> getList(){
        return messageInfoslist;
    }
}
